package com.hua.store.service.impl;

import com.hua.store.dao.ProductDao;
import com.hua.store.dao.impl.ProductDaoImpl;
import com.hua.store.domin.PageModel;
import com.hua.store.domin.Product;
import com.hua.store.service.ProductService;

import java.sql.SQLException;
import java.util.List;

public class ProductServiceImpl implements ProductService {

    ProductDao dao = new ProductDaoImpl();

    @Override
    public List<Product> getNewPro() throws SQLException {
        return dao.getNewPro();
    }

    @Override
    public List<Product> getHotPro() throws SQLException {
        return dao.getHotPro();
    }

    @Override
    public Product findProductByPid(String pid) throws SQLException {
        return dao.findProductByPid(pid);
    }

    @Override
    public PageModel findProductsByCidWithPage(String cid, int curNum) throws SQLException {
        int totalProduct = dao.findTotalProduct(cid);
        PageModel pageModel = new PageModel(curNum, 12, totalProduct);
        List list = dao.findProductsByCidWithPage(cid, pageModel.getStartIndex(), pageModel.getPageSize());
        pageModel.setList(list);
        pageModel.setUrl("ProductServlet?method=findProductsByCidWithPage&cid=" + cid);
        return pageModel;
    }

    @Override
    public PageModel findAllProductWithPage(int curNum) throws SQLException {
        int allProduct = dao.findAllProduct();
        PageModel pageModel = new PageModel(curNum, 6, allProduct);
        List list = dao.findAllProductWithPage(pageModel.getStartIndex(), pageModel.getPageSize());
        pageModel.setList(list);
        pageModel.setUrl("AdminProductServlet?method=findAllProductWithPage");
        return pageModel;
    }

    @Override
    public void saveProduct(Product product) throws Exception {
        ProductDao dao = new ProductDaoImpl();
        dao.saveProduct(product);
    }

}
